<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'edit','title'=>'添加新模板'),
	
);

//模板列表
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','groupid','storeid','fields','usedata','alldata','linktitle','linkurl','deleted');
	$list=pdo_getslice('jy_weishop_virtual_temp',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){

		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：所有关联卡密的商品，都会同步更新商品库存！','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'title','title'=>'模板名称','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','op'=>'data_add','title'=>'添加数据'),
			array('type'=>'href','op'=>'data_list','title'=>'查看数据'),
		)),
		'search_one'=>array('name'=>'title','title'=>'模板名称','add'=>false),
		
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_virtual_temp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($item['fields']){
			$item['fields']=json_decode($item['fields'],true);
		}
	}
	else{
		$item=array();
	}
	if(!$item['fields']){
		$item['fields']=array(
			array('kw'=>'key','title'=>''),
		);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回模板列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'groupid','title'=>'分类','type'=>'select','options'=>'group'),
					array('name'=>'title','title'=>'模板名称','type'=>'string'),
					array('name'=>'linktitle','title'=>'自定义链接名称','type'=>'string'),
					array('name'=>'linkurl','title'=>'链接地址','type'=>'link','from'=>'action'),
					array('type'=>'virtual_key','name'=>'fields','title'=>'数据结构'),
				)
			),
		)
	);
	$return['group']=pdo_getall('jy_weishop_virtual_group',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	//print_r($postdata['specs']);exit;
	//exi($postdata['lev_name'],'error');exit;
	$data=array(
		'title'=>trim($postdata['title']),
		'groupid'=>intval($postdata['groupid']),
		'linktitle'=>trim($postdata['linktitle']),
		'linkurl'=>trim($postdata['linkurl']),
		'fields'=>ijson_encode($postdata['fields']),
	);
	
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_virtual_temp',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	else{	
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_virtual_temp',$data);
		pdo_insertid();
	}

	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_virtual_temp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='data_list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['tempid']=intval($_RQ['id']);
	$return['params']['id']=$params['tempid'];
	if(!empty($_RQ['status'])){
		if($_RQ['status']==1){
			$params['mid']=0;
		}else{
			$params['mid >']=0;
		}
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['pvalue'])){
		$params['pvalue like']='%'.trim($_RQ['pvalue']).'%';
		$return['params']['pvalue']=$_RQ['pvalue'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$fields=array('id','mid','fields','createtime');
	$list=pdo_getslice('jy_weishop_virtual_data',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$value=json_decode($item['fields'],true);
			$item=array_merge($value,$item);
			unset($item['fields']);
			$item['status']=$item['mid']?2:1;//2表示已使用，1表示未使用
		}
	}
	$temp_fields=pdo_getcolumn('jy_weishop_virtual_temp',array('id'=>$params['tempid'],'uniacid'=>$_SESSION['uniacid']),'fields');
	$temp_fields=@json_decode($temp_fields,true);
	if(!$temp_fields){
		exi('指定模版不存在或者模版字段未正确设置！','error','goback');
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'data_export',
		'list'=>array('data'=>'list','edit_op'=>'data_edit','del_op'=>'data_del','list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'select','name'=>'status','title'=>'状态','options'=>'statuss','width'=>'20'),
			array('type'=>'string','name'=>'pvalue','title'=>'主键（key）','width'=>'30'),
		),
		
	);
	foreach($temp_fields as $field){
		$htmls['list']['list'][]=array('type'=>'string','name'=>$field['kw'],'title'=>$field['title']);
	}
	$htmls['list']['list'][]=array('type'=>'radio','name'=>'status','title'=>'状态','options'=>'statuss2');
	$return['statuss']=array(
		array('id'=>1,'title'=>'未使用'),
		array('id'=>2,'title'=>'已使用'),
	);
	$return['statuss2']=array(1=>'未使用',2=>'已使用');
	$return['htmls']=$htmls;
	$tabs=array(
		array('op'=>'list','title'=>'返回卡密模版列表'),
		array('op'=>'data_add','title'=>'添加数据','query'=>'id='.$params['tempid']),
	);
	set_html('tabs',$tabs);
	exi($return);
}
if($_RQ['op']=='data_export'){
	//导出卡密数据
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['tempid']=intval($_RQ['id']);
	
	if(!empty($_RQ['status'])){
		if($_RQ['status']==1){
			$params['mid']=0;
		}else{
			$params['mid >']=0;
		}
		
	}
	if(!empty($_RQ['pvalue'])){
		$params['pvalue like']='%'.trim($_RQ['pvalue']).'%';
		
	}
	$fields=array('id','mid','fields','createtime');
	$list=pdo_getall('jy_weishop_virtual_data',$params,$fields);
	if($list){
		foreach($list as &$item){
			$value=unserialize($item['fields']);
			$item=array_merge($value,$item);
			$item['status']=$item['mid']?'已使用':'未使用';//2表示已使用，1表示未使用
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'id'=>'ID',
	);
	$temp_fields=pdo_getcolumn('jy_weishop_virtual_temp',array('id'=>$params['tempid'],'uniacid'=>$_SESSION['uniacid']),'fields');
	$temp_fields=@json_decode($temp_fields,true);
	if(!$temp_fields){
		exi('指定模版不存在或者模版字段未正确设置！','error','goback');
	}
	foreach($temp_fields as $field){
		$header[$field['kw']]=$field['title'];
	}
	cfc('tools')->export2excel($header,$list,'卡密数据');
	exit();
}
if($_RQ['op']=='data_edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择要编辑的数据！','error','goback');
		}
		unset($post['id'],$post['tempid']);
		$update=array(
			'fields'=>serialize($post),
		);
		pdo_update('jy_weishop_virtual_data',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('编辑成功！','','goback');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的数据！','error','goback');
	}
	$item=pdo_get('jy_weishop_virtual_data',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','tempid','fields'));
	$item['fields']=unserialize($item['fields']);
	$item=array_merge($item['fields'],$item);
	unset($item['fields']);
	$temp=pdo_get('jy_weishop_virtual_temp',array('id'=>$item['tempid'],'uniacid'=>$_SESSION['uniacid']));
	$temp['fields']=json_decode($temp['fields'],true);
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(),
	);
	foreach($temp['fields'] as $field){
		$htmls['edit'][]=array('type'=>'string','name'=>$field['kw'],'title'=>$field['title']);
	}
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='data_del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_virtual_data',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	$this->model('order')->up_virtualtotal($id);
	exi('删除成功！');
}
if($_RQ['op']=='data_add'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择模版！','error','goback');
	}
	$return['template_url']=download_url('jy_weishop/goods.virtual_temp/down_template/id='.$id);
	$item=pdo_get('jy_weishop_virtual_temp',array('id'=>$id));
	$return['fields']=json_decode($item['fields'],true);
	if(!$return['fields']){
		exi('请先设置模版字段！','error','goback');
	}
	$return['title']=$item['title'];
	
	$return['addnum']=10;
	$return['item']=array('id'=>$item['id'],'list'=>array(
		array('key'=>''),
	));
	exi($return);
}
if($_RQ['op']=='data_post'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请先选择模版！','error');
	}
	$list=$post['list'];
	foreach($list as $row){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'tempid'=>$id,
			'pvalue'=>$row['key'],
			'fields'=>ijson_encode($row),
			'createtime'=>TIMESTAMP,
		);
		$check=pdo_getcolumn('jy_weishop_virtual_data',array('uniacid'=>$_SESSION['uniacid'],'tempid'=>$id,'pvalue'=>$insert['pvalue']),'id');
		if(!$check){
			pdo_insert('jy_weishop_virtual_data',$insert);
		}else{
			exi("主键值{$insert['pvalue']}已存在！请更换！",'error');
		}
		
	}
	$this->model('order')->up_virtualtotal($id);
	exi('添加成功！','','//data_list/id='.$id);
}

if($_RQ['op']=='data_add_excel'){
	$post=get_postdata();
	if(!$post['id'] || !$post['file']){
		exi('缺少关键参数，导入失败~','error');
	}
	$id=intval($post['id']);
	$item=pdo_get('jy_weishop_virtual_temp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$item || !$item['fields']){
		exit('指定模版不存在或尚未设置模版字段！');
	}
	$fields=json_decode($item['fields'],true);
	$file=cfc('files')->local($post['file']);
	$data=cfc('tools')->importExcel($file);
		if(!$data){
			exi('导入数据读取失败~','error');
		}
		$first=false;
		foreach($data as $row){
			if(!$first){
				$first=true;
				continue;
			}
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'tempid'=>$id,
				'pvalue'=>$row['0'],
				'fields'=>array(),
				'createtime'=>TIMESTAMP,
			);
			foreach($fields as $key=>$field){
				$insert['fields'][$field['kw']]=$row[$key];
			}
			$insert['fields']=ijson_encode($insert['fields']);
			$check=pdo_getcolumn('jy_weishop_virtual_data',array('uniacid'=>$_SESSION['uniacid'],'pvalue'=>$insert['pvalue']),'id');
			if(!$check){
				pdo_insert('jy_weishop_virtual_data',$insert);
			}
	}
	$this->model('order')->up_virtualtotal($id);
	exi('导入成功！','','//data_list/id='.$id);
}
if($_RQ['op']=='data_add_txt'){
	$post=get_postdata();
	if(!$post['id'] || !$post['file']){
		exi('缺少关键参数，导入失败~','error');
	}
	$id=intval($post['id']);
	$item=pdo_get('jy_weishop_virtual_temp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$item || !$item['fields']){
		exit('指定模版不存在或尚未设置模版字段！');
	}
	$fields=json_decode($item['fields'],true);
	if(count($fields)>1){
		exi('该项目KEY大于1，不能用文本导入~','error');
	}
	$file=cfc('files')->local($post['file']);
	$data=file_get_contents($file);
		if(!$data){
			exi('导入数据读取失败~','error');
		}
		$data=str_replace("\r\n","|",$data);
		$data=explode('|',$data);
		foreach($data as $row){
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'tempid'=>$id,
				'pvalue'=>$row,
				'fields'=>array(),
				'createtime'=>TIMESTAMP,
			);
			foreach($fields as $key=>$field){
				$insert['fields'][$field['kw']]=$row;
			}
			$insert['fields']=ijson_encode($insert['fields']);
			$check=pdo_getcolumn('jy_weishop_virtual_data',array('uniacid'=>$_SESSION['uniacid'],'pvalue'=>$insert['pvalue']),'id');
			if(!$check){
				pdo_insert('jy_weishop_virtual_data',$insert);
			}
	}
	$this->model('order')->up_virtualtotal($id);
	exi('导入成功！','','//data_list/id='.$id);
}
if($_RQ['op']=='down_template'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择模版！','error','goback');
	}
	$item=pdo_get('jy_weishop_virtual_temp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$item || !$item['fields']){
		exit('指定模版不存在或尚未设置模版字段！');
	}
	$fields=json_decode($item['fields'],true);
	$header=array();
	foreach($fields as $field){
		$header[$field['kw']]=$field['title'];
	}
	$data=array();
	cfc('tools')->export2excel($header,$data,$item['title'].'数据模版');
	exit();
}